/**
 * show 系统后台文档检索配置服务JS-docSearchService.jsp
 * by date 2018/05/22 guoyanfei
 */
var docSearchService = function () {
    //项目路径
    var strPath = window.document.location.pathname;
    //var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    var postPath = "/gdp";

    //返回上级目录
    $("#btn_back").click(function(){
        window.location.href=postPath + "/prjService/index";
    });

    //文档来源改变操作
    $("#docsource").change(function () {
        if($("#docsource").val() == "1"){
            //本地文档
            $("#ftpinfo").css("display","none");
            $("#ftphost").val("");
            $("#ftpport").val("");
            $("#ftpuser").val("");
            $("#ftppass").val("");
        }else if($("#docsource").val() == "2"){
            //ftp服务器
            $("#ftpinfo").css("display","block");
        }
    });

    //设置任务调度时间
    $("#btn_set").click(function () {
        $("#cron").val($("#cronexpression").val());
        $("#conModul").modal("show");
    });
    $("#btn_affirm").click(function () {
        $("#cronexpression").val($("#cron").val());
        $("#conModul").modal("hide");
    });
    $("#tasktime").click(function () {
        $("#tasktime").find("li").each(function () {
            $(this).click(function () {
                $("#cron").val($(this).attr("time"));
            });
        });
    });

    //保存事件
    $("#btn_save").click(function () {
        var isValidation = dataOption.validateData();
        if(isValidation){
            dataOption.saveOption();
        }
    });

    //数据操作
    var dataOption = {
        saveOption:function () {
            //保存数据
            var editurl="";
            if($("#hid_operate").val() == "add"){
                editurl=postPath+"/system/systemDocument/insertSysDocument";
            }else {
                editurl=postPath+"/system/systemDocument/updateSysDocument";
            }

            var temp={
                guid:$("#guid").val(),
                servicename:$("#servicename").val(),
                docsource:$("#docsource").val(),
                ftphost:$("#ftphost").val(),
                ftpport:$("#ftpport").val(),
                ftpuser:$("#ftpuser").val(),
                ftppass:$("#ftppass").val(),
                docpath:$("#docpath").val(),
                indextype:'searchdocment',
                cronexpression:$("#cronexpression").val(),
                remark:$("#remark").val(),
                pcode:$("#hid_projectid").val(),
                servicetype:$("#hid_servertype").val()
            };

            $.ajax({
                type:'post',
                url:editurl,
                data:JSON.stringify(temp),
                dataType : 'json',
                contentType:"application/json",
                async:false,
                success:function (res) {
                    if(res > 0){
                        showMsg("保存成功!","success");
                    }else {
                        showMsg("保存失败!","error");
                    }
                }
            });
        },
        validateData:function () {
            //验证数据
            if($("#hid_operate").val() == "add") {
                if($("#guid").val() == ""){
                    $("#errorinfo").html("请输入服务ID!");
                    return false;
                }else if(!/[a-z]/.test($("#guid").val())){
                    $("#saveError").html("检索服务ID只能是小写字母!");
                    return false;
                }else {
                    var result = 0;
                    $.ajax({
                        type:'POST',
                        url:postPath+'/system/systemDocument/isExitServiceId',
                        data:{serviceId:$("#guid").val()},
                        async:false,
                        success:function (res) {
                            result = res;
                        }
                    });

                    if(result > 0){
                        $("#errorinfo").html("服务ID已存在，请重新输入!");
                        return false;
                    }else {
                        $("#errorinfo").html("");
                    }
                }
            }

            if($("#servicename").val() == ""){
                $("#errorinfo").html("请输入服务名称!");
                return false;
            }else{
                $("#errorinfo").html("");
            }
            if($("#docsource").val() == "2"){
                if($("#ftphost").val() == ""){
                    $("#errorinfo").html("FTP服务器地址不能为空!");
                    return false;
                }else{
                    $("#errorinfo").html("");
                }
                if($("#ftpport").val() == ""){
                    $("#errorinfo").html("FTP服务器端口号不能为空!");
                    return false;
                }else {
                    var reg = /^[0-9]*$/;
                    if(!reg.test($("#ftpport").val())){
                        $("#errorinfo").html("FTP服务器端口号只能是数字!");
                        return false;
                    }else {
                        $("#errorinfo").html("");
                    }
                }
                if($("#ftpuser").val() == ""){
                    $("#errorinfo").html("FTP登录用户不能为空!");
                    return false;
                }else {
                    $("#errorinfo").html("");
                }
                if($("#ftppass").val() == ""){
                    $("#errorinfo").html("FTP登录密码不能为空!");
                    return false;
                }else {
                    $("#errorinfo").html("");
                }

                //验证FTP服务器是否联通
                var isconn = false;
                $.ajax({
                    type:'post',
                    url:postPath+'/system/systemDocument/ftpconnection',
                    data:{ftpHost:$("#ftphost").val(),ftpPort:$("#ftpport").val(),ftpUser:$("#ftpuser").val(),ftpPass:$("#ftppass").val()},
                    async:false,
                    success:function (res) {
                        isconn = res;
                    }
                });
                if(!isconn){
                    $("#errorinfo").html("FTP服务器连接失败!");
                    return false;
                }else {
                    $("#errorinfo").html("");
                }

            }else if($("#docsource").val() == "1"){
                if($("#docpath").val() == ""){
                    $("#errorinfo").html("文档路径不能为空!");
                    return false;
                }else {
                    $("#errorinfo").html("");
                }
            }

            if($("#cronexpression").val() == ""){
                $("#errorinfo").html("任务调度周期不能为空!");
                return false;
            }else {
                $("#errorinfo").html("");
            }

            return true;
        },
        editdata:function (option) {
            //修改操作
            if(option == "edit"){
                $.ajax({
                    type:'POST',
                    url:postPath+'/system/systemDocument/selectInfoByguid',
                    data:{guid:$("#hid_serviceId").val()},
                    async:false,
                    success:function (res) {
                        if(res != null){
                            $("#guid").val(res.guid);
                            $("#guid").attr("readonly","readonly");
                            $("#servicename").val(res.servicename);
                            $("#docsource").val(res.docsource);
                            if(res.docsource == "1"){
                                //本地文档
                                $("#ftpinfo").css("display","none");
                            }else if(res.docsource == "2"){
                                //FTP服务器
                                $("#ftpinfo").css("display","block");
                            }
                            $("#ftphost").val(res.ftphost);
                            $("#ftpport").val(res.ftpport);
                            $("#ftpuser").val(res.ftpuser);
                            $("#ftppass").val(res.ftppass);
                            $("#docpath").val(res.docpath);
                            $("#cronexpression").val(res.cronexpression);
                            $("#remark").val(res.remark);
                        }
                    }
                });
            }
        }

    };

    return{
        init:function (option) {
            dataOption.editdata(option);
        }
    }

}();

$(function () {
    //参数
    $("#hid_servertype").val(GetQueryString("servertype"));//服务类型 添加使用
    $("#hid_operate").val(GetQueryString("operate")); //操作类型
    $("#hid_serviceId").val(GetQueryString("servercode"));//服务ID  修改使用
    $("#hid_projectid").val(GetQueryString("projectid"));//工程ID 添加修改预留
    docSearchService.init($("#hid_operate").val());
});

function sleep(delay) {
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay) {
        continue;
    }
}

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

function showMsg(text, icon) {
    $.toast({
        text: text,
        icon: icon,
        showHideTransition: 'fade',
        allowToastClose: true,
        hideAfter: 1500,
        stack: false,
        position: 'top-right',
        textAlign: 'left',
        loader: true,
        loaderBg: '#9ec600',
    });
}
